import { Divider, message, Space, Typography } from 'antd';
import React, { useRef } from 'react';
import {
  ActionType,
  PageContainer,
  ProColumns,
  ProTable,
} from '@ant-design/pro-components';
import {
  examineUpdateApplyTeam,
  examineUpdateNoApplyTeam,
  listUpdateCureApplyTeam,
} from '@/services/teamService';

/***
 * 同意申请
 * @param params
 */
const ExamineUpdateApply = async (
  params: TeamCureExamineType.TeamUpdateApplyRequest,
) => {
  const hide = message.loading('同意申请中');
  try {
    await examineUpdateApplyTeam({
      ...params,
    });
    message.success('成功');
  } catch (e: any) {
    message.error('操作失败' + e.message);
  } finally {
    hide();
  }
};

/***
 * 拒绝申请
 * @param params
 */
const ExamineUpdateNoApply = async (
  params: TeamCureExamineType.TeamUpdateNoApplyRequest,
) => {
  const hide = message.loading('拒绝中');
  try {
    await examineUpdateNoApplyTeam({
      ...params,
    });
    message.success('已拒绝');
  } catch (e: any) {
    message.error('操作失败' + e.message);
  } finally {
    hide();
  }
};

/**
 * 队伍公开信息审核 页面
 */
const AdminTeamApplyPage: React.FC<unknown> = () => {
  const actionRef = useRef<ActionType>();
  /***
   * 表格列配置
   */
  const columns: ProColumns<TeamCureExamineType.teamCureExamine>[] = [
    {
      title: 'id',
      dataIndex: 'id',
      valueType: 'index',
    },
    {
      title: '用户id',
      dataIndex: 'userId',
      valueType: 'text',
    },
    {
      title: '队伍id',
      dataIndex: 'teamId',
      valueType: 'text',
    },
    {
      title: '队伍更新的类型',
      dataIndex: 'teamCureCategory',
      valueType: 'text',
      valueEnum: {
        tagName: { text: '队伍名称' },
        teamDescription: { text: '队伍描述' },
        teamAvatarUrl: { text: '队伍图片' },
      },
    },
    {
      title: '更新的信息',
      dataIndex: 'teamInformation',
      valueType: 'text',
    },
    {
      title: '审核情况',
      dataIndex: 'examineState',
      valueType: 'text',
      valueEnum: {
        0: { text: '审核中' },
        1: { text: '审核通过' },
        2: { text: '拒绝' },
      },
    },
    {
      title: '审核消息',
      dataIndex: 'examineInformation',
      valueType: 'text',
    },
    {
      title: '创建时间',
      dataIndex: 'createTime',
      valueType: 'dateTime',
      hideInForm: true,
    },
    {
      title: '操作',
      dataIndex: 'option',
      valueType: 'option',
      render: (_, record) => (
        <Space split={<Divider type="vertical" />}>
          <Typography.Link
            onClick={() =>
              ExamineUpdateApply({
                id: record.id,
              }).then(() => {
                actionRef?.current?.reload();
              })
            }
          >
            通过
          </Typography.Link>
          <Typography.Link
            onClick={() => {
              return ExamineUpdateNoApply({
                id: record.id,
              }).then(() => {
                actionRef?.current?.reload();
              });
            }}
          >
            拒绝
          </Typography.Link>
        </Space>
      ),
    },
  ];
  return (
    <PageContainer>
      <ProTable<TeamCureExamineType.teamCureExamine>
        headerTitle="用户管理"
        actionRef={actionRef}
        rowKey="id"
        search={{
          labelWidth: 'auto',
        }}
        request={async (params, sorter, filter) => {
          const { data, code } = await listUpdateCureApplyTeam({
            ...params,
            // @ts-ignore
            sorter,
            filter,
          });
          return {
            data: data.records || [],
            success: code === 0,
            total: data.total,
          } as any;
        }}
        columns={columns}
      />
    </PageContainer>
  );
};

export default AdminTeamApplyPage;
